package net.mudfox.helper;

import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdapterHelper {
    public static ArrayAdapter<String> getArrayAdapter(Activity activity,
                                                       List<String> items) {
        ArrayAdapter<String> adapter = new ArrayAdapter<>(activity,
                android.R.layout.simple_list_item_1);
        if (items.isEmpty())
            return adapter;

        adapter.addAll(items);
        return adapter;
    }

    public static ArrayAdapter<String> getArrayAdapter(Activity activity,
                                                       JSONArray array) {
        ArrayAdapter<String> adapter = new ArrayAdapter<>(activity,
                android.R.layout.simple_list_item_1);
        if (array.isEmpty())
            return adapter;

        List<String> items = array.toJavaList(String.class);
        adapter.addAll(items);
        return adapter;
    }

    public static SimpleAdapter getTableAdapter(Activity activity,
                                                List<Map<String, Object>> model,
                                                String[] columns) {

        return new SimpleAdapter(activity, model,
                android.R.layout.simple_list_item_2,
                columns,
                new int[]{android.R.id.text1, android.R.id.text2});
    }

    public static SimpleAdapter getTableAdapter(Activity activity,
                                                JSONArray array,
                                                String[] columns) {
        List<Map<String, Object>> tableModel = new ArrayList<>();
        SimpleAdapter adapter = new SimpleAdapter(activity,
                tableModel,
                android.R.layout.simple_list_item_2,
                columns,
                new int[]{android.R.id.text1, android.R.id.text2});
        if (array.isEmpty())
            return adapter;

        for (int i = 0; i < array.size(); i++) {
            JSONObject itemObject = array.getJSONObject(i);
            if (itemObject.isEmpty())
                continue;

            Map<String, Object> item = new HashMap<>();
            for (String key : columns) {
                Object value = itemObject.get(key);
                if (value != null)
                    item.put(key, value);
            }
            if (!item.isEmpty())
                tableModel.add(item);
        }
        return adapter;
    }

    public static JSONArray verifyObject(JSONObject rootObject,
                                         String verifyKey,
                                         String verifyValue,
                                         String fetchKey) {
        if (!rootObject.getString(verifyKey).equals(verifyValue))
            return new JSONArray();

        if (rootObject.containsKey(fetchKey))
            return rootObject.getJSONArray(fetchKey);
        else
            return new JSONArray();
    }

    public static JSONArray verifyObject(JSONObject rootObject,
                                         String verifyValue) {
        return AdapterHelper.verifyObject(rootObject, "entry", verifyValue, "detail");
    }

    public static SimpleAdapter getSimpleAdapter(Activity activity,
                                                 JSONArray rootArray,
                                                 int resId,
                                                 String[] columns,
                                                 int[] res) {
        List<Map<String, Object>> tableModel = new ArrayList<>();
        SimpleAdapter adapter = new SimpleAdapter(activity, tableModel, resId, columns, res);
        if (rootArray.isEmpty())
            return adapter;

        for (int i = 0; i < rootArray.size(); i++) {
            JSONObject itemObject = rootArray.getJSONObject(i);
            if (itemObject.isEmpty())
                continue;

            Map<String, Object> item = new HashMap<>();
            for (String key : columns) {
                Object value = itemObject.get(key);
                if (value != null)
                    item.put(key, value);
            }
            if (!item.isEmpty())
                tableModel.add(item);
        }
        return adapter;
    }
}
